<?php
namespace Modules\Api\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Address;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AddressController extends Controller{

    //查询默认收货地址
    public function selected(Request $request){
        try{
            $openid = $request->input("openid",false);
            if (!$openid)return self::error(1000);
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            $address = Address::where("user_id",$user->id)->where("selected",1)->first();
            if (!$address)return self::error(1003);
            $data = ["id"=>$address->id,"username"=>$address->username,"phone"=>$address->phone,"province"=>$address->province,"city"=>$address->city,"area"=>$address->area,"address"=>$address->province.$address->city.$address->area.$address->detail,"selected"=>$address->selected];
            return self::success(["address"=>$data]);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //我的收货地址
    public function list(Request $request){
        try{
            $openid = $request->input("openid",false);
            if (!$openid)return self::error(1000);
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            $address = Address::where("user_id",$user->id)->orderBy("id","desc")->get();
            if ($address->count()>0){
                $data = [];
                foreach ($address as $item){
                    $data[] = ["id"=>$item->id,"username"=>$item->username,"phone"=>$item->phone,"province"=>$item->province,"city"=>$item->city,"area"=>$item->area,"address"=>$item->province.$item->city.$item->area.$item->detail,"selected"=>$item->selected];;
                }
                return self::success(["list"=>$data]);
            }else{
                return self::error(1003);
            }
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //新增收货地址
    public function add(Request $request){
        try{
            $openid = $request->input("openid",false);
            $username = $request->input("username",false);
            $phone = $request->input("phone",false);
            $province = $request->input("province",false);
            $city = $request->input("city",false);
            $area = $request->input("area",false);
            $detail = $request->input("detail");
            $selected = $request->input("selected",0);
            if (!$openid || !$username || !$phone || !$province || !$city || !$area)return self::error(1000);
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            //修改默认地址
            if ($selected==1){
                Address::where("user_id",$user->id)->update(['selected'=>0]);
            }
            //添加地址
            $address = new Address();
            $address->user_id = $user->id;
            $address->username = $username;
            $address->phone = $phone;
            $address->province = $province;
            $address->city = $city;
            $address->area = $area;
            $address->detail = $detail;
            $address->selected = $selected;
            $address->created_at = Carbon::now();
            $address->save();
            return self::success();

        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //编辑收货地址
    public function edit(Request $request){
        try{
            $id = $request->input("id",false);
            $username = $request->input("username",false);
            $phone = $request->input("phone",false);
            $province = $request->input("province",false);
            $city = $request->input("city",false);
            $area = $request->input("area",false);
            $detail = $request->input("detail");
            $selected = $request->input("selected",0);
            if (!$id || !$username || !$phone || !$province || !$city || !$area)return self::error(1000);
            $address = Address::find($id);
            if (!$address)return self::error(1003);
            //修改默认地址
            if ($selected==1){
                Address::where("user_id",$address->user_id)->update(['selected'=>0]);
            }
            //添加地址
            $address->username = $username;
            $address->phone = $phone;
            $address->province = $province;
            $address->city = $city;
            $address->area = $area;
            $address->detail = $detail;
            $address->selected = $selected;
            $address->created_at = Carbon::now();
            $address->save();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除收货地址
    public function delete(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $address = Address::find($id);
            if (!$address)return self::error(1003);
            $address->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //设为默认地址
    public function default(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $address = Address::find($id);
            if (!$address)return self::error(1003);
            DB::beginTransaction();
            Address::where("user_id",$address->user_id)->update(["selected"=>0]);
            $address->selected = 1;
            $address->save();
            DB::commit();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            DB::rollback();
            return self::error(500);
        }
    }
}